package com.shuoyue.ycgk.ui.estimate;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.Rule;
import com.shuoyue.ycgk.ui.common.RuleContract;
import com.shuoyue.ycgk.ui.questionbank.QuestionUtil;
import com.shuoyue.ycgk.ui.questionbank.practice.estamate.EstamateCountActivity;
import com.shuoyue.ycgk.utils.ShareUtil;
import com.shuoyue.ycgk.views.TabLayoutUtil;
import com.shuoyue.ycgk.views.dialog.common.BigHintDialog;
import com.shuoyue.ycgk.views.dialog.common.HintDialog;
import com.shuoyue.ycgk.views.viewpager_scroll.SuperViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EsSourtActivity extends BaseMvpActivity<EstimateCheckAllContract.Presenter>
        implements EstimateCheckAllContract.View, RuleContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    SuperViewPager viewPager;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    RuleContract.Presenter rulePrenter;
    String name;

    public static void start(Context context, int evaId, int testId, String name) {
        context.startActivity(new Intent(context, EsSourtActivity.class)
                .putExtra("evaId", evaId).putExtra("name", name)
                .putExtra("testId", testId));
    }


    protected IndexFragmentStateAdapter indexFragmentAdapter;
    protected List<BaseMvpFragment> fragmentMainItems;

    int evaId, testId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_es_sourt;
    }

    @Override
    protected void initView() {
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.share_white);
        evaId = getIntent().getIntExtra("evaId", -1);
        testId = getIntent().getIntExtra("testId", -1);
        name = getIntent().getStringExtra("name");
        pageTitle.setText("排行榜");
        fragmentMainItems = new ArrayList<>();
        fragmentMainItems.add(FragmentSoutByJob.getInstance(evaId));
        fragmentMainItems.add(FragmentSoutAll.getInstance(evaId));

        indexFragmentAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(indexFragmentAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new EstimateCheckAllContract.Presenter();
        mPresenter.attachView(this);
        rulePrenter = new RuleContract.Presenter();
        rulePrenter.attachView(this);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void setQuestions(List<QuestionParent> questions) {
        QuestionUtil.repackQuestionParent(questions, true);
        EstamateCountActivity.start(mContext, name, (ArrayList<QuestionParent>) questions);
    }


    @OnClick({R.id.rulls, R.id.checkAll, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rulls:
                rulePrenter.getRule(4);

                break;
            case R.id.checkAll:
                mPresenter.getQuestions(testId, evaId);
                break;
            case R.id.iv_right:
                ShareUtil.shareUrl(this,
                        ShareUtil.ESTIMATE_URL + evaId,
                        "估分排行",
                        "",
                        null);
                break;
        }
    }


    @Override
    public void setRule(Rule userInfo) {
        BigHintDialog hintDialog = new BigHintDialog(mContext, "规则说明",
                userInfo.getContent(), Gravity.LEFT);
        hintDialog.show();
    }
}
